package com.yan.club.apply.control;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yan.club.apply.dto.ClubApplyDto;
import com.yan.club.apply.model.ClubApply;
import com.yan.club.apply.service.ClubApplyService;
import com.yan.club.result.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yan.club.auth.utils.SecurityUtil.*;

/**
 * @author 彦
 * @since 2024/1/10 9:18
 */
@RestController
@RequestMapping("/apply")
public class ClubApplyController {

    @Resource
    private ClubApplyService clubApplyService;

    /**
     * 查询
     */
    @PostMapping("/list")
    public Result<PageInfo<ClubApply>> list(@RequestBody ClubApplyDto clubApplyDto) {

        Long userId = getUserId();
        if (isAdmin(userId)) {
            PageHelper.startPage(clubApplyDto.getPageNum(), clubApplyDto.getPageSize(), true);
            List<ClubApply> list = clubApplyService.list(clubApplyDto);
            PageInfo<ClubApply> clubApplyPageInfo = new PageInfo<>(list);
            return Result.success(clubApplyPageInfo);
        } else {
            // 得到学号
            String userName = getUser().getUserName();
            PageHelper.startPage(clubApplyDto.getPageNum(), clubApplyDto.getPageSize(), true);
            List<ClubApply> list = clubApplyService.getApplyByStudentNo(clubApplyDto, userName);

            PageInfo<ClubApply> clubApplyPageInfo = new PageInfo<>(list);
            return Result.success(clubApplyPageInfo);
        }


    }

    /**
     * 新增
     *
     * @param clubApply
     * @return
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody ClubApply clubApply) {
        return clubApplyService.save(clubApply);
    }

    /**
     * 修改状态
     *
     * @param applyId
     * @return
     */
    @PostMapping("/updateStatus")
    public Result<String> updateStatus(@RequestParam Long applyId, @RequestParam Integer status) {
        return clubApplyService.updateStatus(applyId, status);
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{applyId}")
    public Result<String> delete(@PathVariable Long applyId) {
        return clubApplyService.delete(applyId);
    }

//    @GetMapping("/{studentNo}")
//    public Result<ClubApply> getApplyByStudentNo(@PathVariable String studentNo) {
//        return clubApplyService.getApplyByStudentNo(studentNo);
//    }
}

